﻿#ifndef IMGPRODCONS_H
#define IMGPRODCONS_H

#include"Camera.h"
#include"Postion.h"
#include"armordetection.h"
#include"serialport.h"
#include<chrono>
#include<mutex>
#include<memory>
#include<QCoreApplication>
#include"controal.h"

struct Frame
{
    cv::Mat img;
    size_t seq;         //count from 1
    double timeStamp;	//time in ms, from initialization to now
};

/*
 * @Brief:  A simple circular buffer designed only for buffering captured images.
 *          A new frame will cover the oldest one. Every frame has its own mutex
 *          and the mutex is locked when the frame is being updated or being achieved
 *          to ensure thread safety.
 */
class FrameBuffer
{
public:
    FrameBuffer(size_t size);

    ~FrameBuffer() = default;

    bool push(const Frame& frame);

    bool getLatest(Frame& frame);

private:

    std::vector<Frame> _frames;
    std::vector<std::timed_mutex> _mutexs;

    size_t _tailIdx;
    size_t _headIdx;

    double _lastGetTimeStamp;
};



class ImgProdCons :public QObject
{
    Q_OBJECT
public:
    ImgProdCons();
    void produce();//生产图片线程
    void consume();//处理图片线程
       bool flage=0;
    void init();
    void debugser();
    void send_thread();//发送线程

private:
     //controal _control;
     FrameBuffer _buffer;
     std::unique_ptr<Camera> _Camera;
     std::unique_ptr<Solve_Posetion>_Solve_Posetion;
     std::unique_ptr<ArmorDetection>_ArmorDetection;
     std::unique_ptr<controal> _Controal;


//public slots:
     //void onReadyRead();
};
class ourthread :public QThread
{
    Q_OBJECT
public:
    ourthread(ImgProdCons *p,int cmd);
protected:
    void run();
private:
    ImgProdCons *ipc;
    int cmd;

};
#endif // IMGPRODCONS_H
